"use strict";

/**
 * 随机字符串生成工具
 * @param AssignMakeLength 指定需要生成的字符串的长度
 * @return string
 * */
function randomStringMaker(AssignMakeLength) {
    let StringLibrary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()-=[]{}\\|:;'\"/?,.<>`+_"; //数字、英文字母大小写字符库
    let SaveFinalMakeString = ""; //保存最终生成字符串
    let RandomCharacterSelectIndex = 0; //随机字符选择索引
    let DefaultLength = 7; //默认生成长度、当AssignMakeLength未指定或为0或undefined时，启用
    if (AssignMakeLength <= 0 || AssignMakeLength === "" || AssignMakeLength === undefined) {
        AssignMakeLength = DefaultLength; //赋值形参默认生成长度
    }
    //开始使用for循环生成随机字符
    for (let i = 0; i < AssignMakeLength; i++) {
        RandomCharacterSelectIndex = parseInt(93 * Math.random() + ""); //随机生成整数型字符索引号
        SaveFinalMakeString = SaveFinalMakeString + StringLibrary.charAt(RandomCharacterSelectIndex); //从字符库选取字符并累加到最终生成字符串
    }
    return SaveFinalMakeString;
}

/**
 * 随机字符串生成工具Ex
 * ```
 * ```
 * @param {{
 *     BuilderLength:number,
 *     EnableUpperEng:boolean,
 *     EnableLowerEng:boolean,
 *     EnableEngSymbol:boolean,
 *     EnableNumberText:boolean
 * }} ParameterObject 参数对象
 * ```
 * ```
 * @return string
 *
 * ```
 * ```
 * @description ParameterObject 参数对象属性
 * @description BuilderLength: 指定生成长度
 * @description EnableUpperEng: 启用大写字母随机生成,
 * @description EnableLowerEng: 启用小写字母随机生成,
 * @description EnableEngSymbol: 启用英文标点符号随机生成,
 * @description EnableNumberText: 启用阿拉伯数字字符随机生成,
 *```
 * @example 用法：console.log(当前构建的类实例对象.randomStringMakerEx({
 * BuilderLength: 1,
 * EnableUpperEng: false,
 * EnableLowerEng: false,
 * EnableEngSymbol: false,
 * EnableNumberText: true
 * })
 * ```
 * */
function randomStringMakerEx(ParameterObject) {
    //字符库定义
    let lowerCaseLibrary = "abcdefghijklmnopqrstuvwxyz"; //小写字母
    let upperCaseLibrary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //大写字母
    let engSymbol = "~!@#$%^&*()-=[]{}\\|:;'\"/?,.<>`+_"; //英文标点符号
    let numberSymbolText = "0123456789"; //数字字符
    let genericLibrary = lowerCaseLibrary + upperCaseLibrary + engSymbol + numberSymbolText;
    //字符库定义

    //参数对象属性
    let makeLength = 0; //初始生成长度
    let enableLowerEngText = false; //启用小写字母
    let enableUpperEngText = false; //启用大写字母
    let enableEngSymbol = false; //启用英文标点符号
    let enableNumberText = false; //启用数字字符
    //参数对象属性

    /**函数的运行状态**/
    let execAttrCheck = true; //启用属性检查循环
    let parameterError = false; //记录参数对象是否有错
    /**函数的运行状态**/

    let finalMake = ""; //最终生成
    switch (arguments.length) {
        case 0:
            execAttrCheck = false; //阻止下面的for循环的执行
            makeLength = 7; //赋予默认生成长度
            break;
        case 1:
            if (this.emptyObjectDetect(ParameterObject)) {
                //判断形参对象是否为空
                execAttrCheck = false; //阻止下面的for循环的执行
                makeLength = 7; //赋予默认生成长度
            } else if (this.emptyObjectDetect(ParameterObject) === undefined) {
                execAttrCheck = false; //阻止下面的for循环的执行
                makeLength = 7; //赋予默认生成长度
            }
            break;
        default:
            throw new Error("仅能接受一个形参！");
    }
    for (let attrName in ParameterObject) {
        if (execAttrCheck === false) {
            break;
        }
        //此循环开始遍历ParameterObject对象里的所有属性，并赋值到变量
        switch (attrName.toLowerCase()) {
            case "builderlength":
                makeLength = ParameterObject[attrName];
                break;
            case "enableuppereng":
                enableUpperEngText = ParameterObject[attrName];
                break;
            case "enablelowereng":
                enableLowerEngText = ParameterObject[attrName];
                break;
            case "enableengsymbol":
                enableEngSymbol = ParameterObject[attrName];
                break;
            case "enablenumbertext":
                enableNumberText = ParameterObject[attrName];
                break;
            default:
                parameterError = true;
                break;
        }
        if (parameterError === true) {
            break;
        } //如果parameterError为true，终止循环
    }
    if (makeLength <= 0 || parameterError === true) {
        //判断是否符合条件，如不符合，直接结束本次函数运行
        return null;
    }
    //开始循环随机生成
    while (true) {
        if (enableLowerEngText === true) {
            finalMake = finalMake + lowerCaseLibrary.charAt(parseInt((Math.random() * 26) + ""));
        }
        if (enableUpperEngText === true) {
            finalMake = finalMake + upperCaseLibrary.charAt(parseInt((Math.random() * 26) + ""));
        }
        if (enableEngSymbol === true) {
            finalMake = finalMake + engSymbol.charAt(parseInt((Math.random() * 32) + ""));
        }
        if (enableNumberText === true) {
            finalMake = finalMake + numberSymbolText.charAt(parseInt((Math.random() * 10) + ""));
        }
        if (!enableEngSymbol && !enableLowerEngText && !enableUpperEngText && !enableNumberText) {
            finalMake = finalMake + genericLibrary.charAt(parseInt((Math.random() * 94) + ""));
        }
        if (finalMake.length >= makeLength) {
            break;
        }
    }
    return finalMake;
}

/**
 * 随机选择布尔值
 * @return boolean
 * */
function randomSelectBoolean() {
    return parseFloat(Math.random().toString()).toFixed(1) >= 0.5;
}

/**
 * 随机生成MAC地址
 * @param splitSymbol 指定分隔符号，仅支持 英文的冒号（:）、横杠（-）、下划线（_）
 * @param useUpper 是否全大写英文字母
 *
 * @return string
 *
 * @example 生成全大写英文字母的MAC地址，当前构建的类实例对象.randomMacGenerator('-',true)
 * @example 生成全小写英文字母的MAC地址，当前构建的类实例对象.randomMacGenerator('-',false)
 * @example 生成MAC地址、默认全小写英文字母，当前构建的类实例对象.randomMacGenerator('-')
 * */
function randomMacGenerator(splitSymbol, useUpper) {
    let finalConfirmSplit;
    switch (String(splitSymbol).toString()) {
        case ":":
            finalConfirmSplit = ":";
            break;
        case "-":
            finalConfirmSplit = "-";
            break;
        case "_":
            finalConfirmSplit = "_";
            break;
        default:
            if (typeof splitSymbol === "boolean") {
                useUpper = splitSymbol;
            }
            finalConfirmSplit = ":";
            break;
    }
    let finalMakeMacAddressArr = [];

    function randomHex(EnaUpper) {
        let makeValue;
        let lowerString = "0123456789abcdef";
        let upperString = "0123456789ABCDEF";
        let SelectNum = parseInt(String(16 * Math.random()).toString());
        if (EnaUpper) {
            makeValue = String(upperString).charAt(SelectNum);
            SelectNum = parseInt(String(16 * Math.random()).toString());
            makeValue = makeValue + String(upperString).charAt(SelectNum);
        } else {
            makeValue = String(lowerString).charAt(SelectNum);
            SelectNum = parseInt(String(16 * Math.random()).toString());
            makeValue = makeValue + String(lowerString).charAt(SelectNum);
        }
        return makeValue;
    }

    if (typeof useUpper !== "boolean") {
        useUpper = false;
    }
    for (let count = 0; count < 6; count++) {
        finalMakeMacAddressArr[count] = randomHex(useUpper);
    }
    return finalMakeMacAddressArr.join(finalConfirmSplit);
}

module.exports = {randomStringMaker, randomStringMakerEx, randomSelectBoolean, randomMacGenerator};